using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Xml;
using Graphe.Tools.TextualRangeGuesser;

namespace Graphe.WPF
{
    /// <summary>
    /// Interaction logic for Tools.xaml
    /// </summary>

    public partial class Tools : System.Windows.Window
    {
        #region Constructor

        public Tools()
        {
            InitializeComponent();
        }

        #endregion
        #region Private Instance Fields

        private Index _index;

        #endregion
        #region Public Properties

        public Index Index
        {
            get { return _index; }
            set { _index = value; }
        }

        #endregion


        private void _buttonWordCounter_Click(object sender, RoutedEventArgs e)
        {
            TextualRangeGuesser guesser = new TextualRangeGuesser();
            guesser.Show();
        }

        private void _buttonExportIndex_Click(object sender, RoutedEventArgs e)
        {
            if (_index == null)
                return;

            System.Windows.Forms.SaveFileDialog dlg = new System.Windows.Forms.SaveFileDialog();
            dlg.InitialDirectory = Settings.Instance.LastIndexPath;

            if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
            {
                _index.Export(dlg.FileName);
            }
        }

        private void _buttonCreateIndexFromFolder_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();

            if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
            {
                string[] fileNames = Directory.GetFiles(dlg.SelectedPath);
                SortedList<string, string> sortedFiles = new SortedList<string, string>();

                foreach (string fileName in fileNames)
                {
                    string extension = Path.GetExtension(fileName).ToLower();
                    if (extension == ".jpg" || extension == ".jpeg" || extension == ".gif" || extension == ".tiff")
                    {
                        sortedFiles.Add(fileName, fileName);
                    }
                }

                using(XmlTextWriter writer = new XmlTextWriter(Path.Combine(dlg.SelectedPath, "NewIndex.xml"), System.Text.Encoding.Unicode))
                {
                    writer.WriteStartElement("Index");
                    {
                        writer.WriteStartElement("Metadata");
                        {
                            writer.WriteStartElement("Name");
                            writer.WriteString("New Index Name");
                            writer.WriteEndElement();
                        }
                        writer.WriteEndElement();

                        writer.WriteStartElement("ImageReferences");

                        int i = 1;
                        foreach (string fileName in sortedFiles.Keys)
                        {
                            writer.WriteStartElement("Image");

                            writer.WriteAttributeString("Number", i.ToString());
                            writer.WriteAttributeString("FileName", Path.Combine(dlg.SelectedPath, fileName));
                            writer.WriteAttributeString("StartReference", String.Empty);

                            writer.WriteEndElement();

                            i++;
                        }

                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();

                }

                XmlDocument doc = new XmlDocument();
                doc.Load(Path.Combine(dlg.SelectedPath, "NewIndex.xml"));
                doc.Save(Path.Combine(dlg.SelectedPath, "NewIndex.xml"));
            }
        }

    }
}